var api = require('../common/config.js')
var util = require('../common/utils.js')
Page({
    data: {
        operator: ''       //操作员
        , expressNumber: '' //快递单号
        , customerName: ''  //客户姓名
        , mobile: ''        //手机号
        , address: ''       //联系地址
        , buyDate: ''       //购买日期
        , status: ''        //订单状态
        , businessInfo: ''  //商户信息
        , customer_car_info:"" // 车型颜色
        , uid: ''
        , token: ''
    }
    //页面初始化调用
    , getData: function () {
        let that = this;
        wx.getStorage({
            key: 'data',
            success: function (res) {
                var data = new Object();
                data.operator = res.data.user.username;
                data.uid = res.data.user.id;
                data.token = res.data.token;
                that.setData(data); // 重新刷新当前页面
            }
        })
    }
    , btnScanVerifyInfo: function (e) {//扫码确认订单信息
        let that = this;
        let status = that.data.status;
        if (!status) {
            wx.showToast({
                title: '状态为空不能提交',
                icon: 'none',
                duration: 1000,
                mask: true
            });
            return false;
        } else if (status != '制作中') {
            wx.showToast({
                title: '状态非制作中不能提交',
                icon: 'none',
                duration: 1000,
                mask: true
            });
            return false;
        }
        var expressNumber = that.data.expressNumber;
        var uid = that.data.uid;
        var token = that.data.token;
        console.log(expressNumber + " " + uid + " " + token + " " + api.getOrderInfo)
        util.request(api.submitOrder, {"danhao": expressNumber, "uid": uid, "token": token}).then(function (res0) {
            console.log(res0)
            wx.hideLoading();
            if (res0.code == 1) {
                var data = new Object();
                var order = res0.data.order;
                data.expressNumber = expressNumber;
                data.customerName = order.customer_name;
                data.mobile = order.customer_mobile;
                data.address = order.customer_order_address;
                data.buyDate = res0.data.buyDate;
                data.status = res0.data.orderStatus;
                data.businessInfo = order.business_info;
                data.customer_car_info = order.customer_car_info;
                that.setData(data);
                wx.showToast({
                    title: '订单操作成功',
                    icon: 'succes',
                    duration: 1000,
                    mask: true,
                    success: function () {
                        wx.navigateTo({
                            url: '/pages/result/result',
                        })
                    }
                });
            } else {
                var msg1 = res0.msg;
                wx.showModal({
                    title: '错误信息',
                    content: msg1,
                    showCancel: false
                });
            }
        });
    },
    btnScanGetOrderInfo: function (e) {// 扫码获取订单信息
        wx.scanCode({
            scanType: ["barCode"],
            success: (res) => {
                console.log(res);
                wx.showLoading({
                    title: '加载中...',
                });
                let that = this;
                var expressNumber = res.result;
                var uid = that.data.uid;
                var token = that.data.token;
                console.log(expressNumber + " " + uid + " " + token + " " + api.getOrderInfo)
                util.request(api.getOrderInfo, {
                    "danhao": expressNumber,
                    "uid": uid,
                    "token": token
                }).then(function (res0) {
                    console.log(res0)
                    wx.hideLoading();
                    if (res0.code == 1) {
                        var data = new Object();
                        var order = res0.data.order;
                        data.expressNumber = expressNumber;
                        data.customerName = order.customer_name;
                        data.mobile = order.customer_mobile;
                        data.address = order.customer_order_address;
                        data.buyDate = res0.data.buyDate;
                        data.status = res0.data.orderStatus;
                        data.businessInfo = order.business_info;
                        data.customer_car_info = order.customer_car_info;
                        that.setData(data);
                    } else {
                        var msg1 = res0.msg;
                        wx.showModal({
                            title: '错误信息',
                            content: msg1,
                            showCancel: false
                        });
                    }
                });
            },
            fail: (res) => {
                console.log(res);
                wx.showModal({
                    title: '扫描错误',
                    content: "扫描错误",
                    showCancel: false
                });
            }
        })
    }
    , onLoad: function (options) {
        // 页面初始化 options为页面跳转所带来的参数
        this.getData();
    },
    onReady: function () {
        // 页面渲染完成
    },
    onShow: function () {
        // 页面显示

    },
    onHide: function () {
        // 页面隐藏

    },
    onUnload: function () {
        // 页面关闭
    }
});
